# -*- coding: utf-8 -*-


from facility import logger
import time
import redis
import json
from facility.http_fn import post


class DbPipeline(object):
    def __init__(self):
        self.redis = redis.Redis(host='lh', port=6379, db=0, password='@2CGFSM4tfLUNJkXMQeEJ')

    @classmethod
    def from_crawler(cls, crawler):
        instance = cls()
        return instance

    def process_item(self, item, spider):
        key = spider.name + '_data'

        if not self.redis.hexists(key, item["id"]):
            item["add_time"] = int(time.time() * 1000)
            self.redis.hset(key, item["id"], json.dumps(item, ensure_ascii=False))

            post('http://localhost:10001/msg', json={'channel': 'desktop',
                                                     'title': spider.name,
                                                     'content': json.dumps(item, ensure_ascii=False)})
